package com.example.demo.jdk.jvm.classloader;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: htc
 * @date: Created in 14:16 2018/9/26.
 */
public class ClassLoaderDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        ClassLoader loader = new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                try {
                    String fileName = name.substring(name.lastIndexOf(".") + 1);
                    InputStream is = getClass().getResourceAsStream(fileName);
                    if (null == is) {
                        return super.loadClass(name);
                    }

                    byte[] bytes = new byte[is.available()];
                    is.read(bytes);
                    return defineClass(name, bytes,0, bytes.length);
                } catch (IOException e) {
                    throw new ClassNotFoundException(name);
                }
            }
        };
        Object obj = loader.loadClass("com.iresearch.idata.swap.jvm.classloader.ClassLoaderDemo2").newInstance();
        System.out.println(obj.getClass());
//        System.out.println(obj instanceof com.iresearch.idata.swap.jvm.classloader.ClassLoaderDemo2);
    }
}
